package indi.kider.server.applandlord.api;

import indi.kider.common.model.apabus.HouseCount;
import indi.kider.common.model.apartment.Houses;
import indi.kider.common.model.apartment.HousesMsg;
import indi.kider.server.applandlord.service.related.ApartmentService;
import indi.kider.server.applandlord.service.related.HousesInfer;
import indi.kider.server.applandlord.service.related.LockService;
import indi.kider.server.applandlord.service.related.RentIntface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;

import java.util.List;

/**
 * Created by shixj on 2018/5/30.
 */
@RequestMapping(value = "houses", produces = "application/json;charset=UTF-8")
@RestController
public class HousesController {
    @Autowired
    private HousesInfer housesInfer;
    @Autowired
    private LockService lockService;
    @Autowired
    private RentIntface rentIntface;
    @Autowired
    private ApartmentService apartmentService;
    /**
     * @api {GET} houses/queryHouseCount
     * @apiGroup app-landlord
     * @apiDescription 查询小区信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-landlord/houses/queryHouseCount
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "200",
     * "msg": "查询成功！",
     * "data":   [{
     * "allCount": 10,
     * "vacant": 8
     * }]
     * }
     */
    @GetMapping("/queryHouseCount")
    public CommonResponse<HouseCount> queryHouseCont(HttpRequest httpRequest) {
        return rentIntface.houseCount(httpRequest);
    }

    /**
     * @api {POST} /houses
     * @apiGroup app-landlord
     * @apiDescription 新增房屋
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-landlord/houses
     * {"area":null,"building":null,"cnt1":null,"cnt2":null,"communityId":null,"deposit":null,"devs":null,"e1url":null,"e2url":null,"gasCost":null,"h1url":null,"h2url":null,"h3url":null,"h4url":null,"heading":null,"houseNumb":null,"id":null,"netCost":null,"outCode":null,"powerCost":null,"powerMeter":null,"priceSet":null,"propertyCosts":null,"state":null,"unit":null,"waterCost":null,"waterMeter":null}
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "201",
     * "msg": "保存成功！",
     * "data": 1
     * }
     */
    @PostMapping("")
    public CommonResponse addHouse(@RequestBody Houses houses) {
        /*需要增加房东和房源的信息. */
        return housesInfer.addHouse(houses);
    }

    /**
     * @api {POST} houses/list
     * @apiGroup app-landlord
     * @apiDescription 查询房源列表
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-landlord/houses/list
     * {"num":1,"size":8,"query":"18013897594"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":82,"communityId":15,"houseNumb":"WN_0005","building":null,"unit":null,"area":2.0,"heading":"东","cnt1":1,"cnt2":1,"e2url":null,"e1url":"","h4url":null,"h3url":null,"h2url":null,"h1url":"","outCode":null,"waterMeter":null,"powerMeter":null,"priceSet":1100.0,"setLease":1,"deposit":1000.0,"state":2,"devs":"1,5","netCost":2.0,"propertyCosts":2.0,"powerCost":10.0,"waterCost":10.0,"shareCost":10.0,"gasCost":null,"city":"天津","areas":"河东区","community":"日日公寓","housesNumbs":0,"address":"日日鲁嘻嘻","coordinate":null,"remark":"","aroundDirs":"","tenantName":"wbb","tenantTel":"13333333333","cardId":"44","tenantId":69,"rent":{"id":105,"housesId":82,"communityId":15,"tenantId":69,"rentDate":"2018-05-28","state":1,"remark":"44","priceSet":1100.0,"setLease":1,"leaseType":null,"deposit":1000.0,"depositRemark":null,"initWaterReading":10.0,"initPowerReading":10.0,"payCycle":1,"wxopenId":null,"tenantName":"wbb","tenantTel":"13333333333","houseNumb":"WN_0005","building":null,"unit":null,"address":"日日鲁嘻嘻","community":"日日公寓","cardId":"44","netCost":2.0,"propertyCosts":2.0,"powerCost":10.0,"waterCost":10.0,"gasCost":null,"nationality":null,"professional":null,"friendsSize":1},"friends":[{"id":74,"name":"shixj1","cardId":"12212","phone":"1221","title":null,"nationality":"32","professional":"23","wxopenId":null,"state":1,"type":2,"friendsSize":null,"mainId":69}],"friendsSize":1,"devLock":{"id":12,"houseId":null,"partnerCode":null,"name":"555","code":null,"imei":"55","cert":null,"regCode":null,"state":1,"createDate":null}}],"total":5,"size":5}}
     */
    @PostMapping("list")
    public CommonResponse getTenantInfo(@RequestBody PageRequest<String> request) {
        CommonResponse<PageResponse<List<HousesMsg>>> commonResponse=housesInfer.selectHouseMsgByEnterprise(request);
        commonResponse.getData().getRows().stream().forEach(housesMsg -> {
            if(housesMsg.getId()!=null&&housesMsg.getId()>0)
                housesMsg.setDevLock(lockService.lockByHouseId(housesMsg.getId()).getData());
        });
        return commonResponse;
    }




    /**
     * @api {PUT} /houses
     * @apiGroup app-landlord
     * @apiDescription 修改房屋信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-landlord/houses
     * {
     * "area": null,
     * "building": null,
     * "cnt1": null,
     * "cnt2": null,
     * "communityId": null,
     * "deposit": null,
     * "e1url": null,
     * "e2url": null,
     * "h1url": null,
     * "h2url": null,
     * "h3url": null,
     * "h4url": null,
     * "heading": null,
     * "houseNumb": null,
     * "id": 1,
     * "outCode": 12,
     * "powerMeter": null,
     * "priceSet": null,
     * "unit": null,
     * "waterMeter": null
     * }
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "202",
     * "msg": "保存成功！",
     * "data": 1
     * }
     */
    @PutMapping("")
    public CommonResponse modifyHouses(@RequestBody Houses houses) {
        return apartmentService.modifyHouses(houses);
    }

}
